<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\controller\Common;
use think\Db;

class Recharge extends Common
{
    /**
     * User:Simony
     * Function: 提现管理
     * @return mixed
     */
    public function index()
    {

        //params
        $field = input('get.field', '');
        $value = input('get.value', '');
        $search_field = input('get.search_field', '');
        $search_value = input('get.search_value', '');

        //where
        $where = [];

        //judge
        $field && $value && $where[$field] = $value;
        $search_field && $search_value &&  $where[$search_field] = $search_value;

        //model
        $rechargeLogModel = model('common/member/RechargeLog');

        //getData
        $field = 'r.*,m.nickname,m.username';
        $data = $rechargeLogModel->getRechargeLogList($where, 10, $field);

        //assign
        $this->assign('data', $data);
        $this->assign('states', $rechargeLogModel->states);
        $this->assign('recharge_types', $rechargeLogModel->recharge_types);
        $this->assign('page', $rechargeLogModel->page_info->render());
        return $this->fetch();
    }

    public function recharge()
    {
        $member_id = input('get.member_id', 1);

        $memberModel = model('common/member/Member');
        $member = $memberModel->getInfoByID($member_id);

        if ($this->request->isAjax()) {
//            ds_json_encode('err', '代码未写完');

            $memberId = input('post.member_id/d');
            $money = input('post.money/d');
            $des = input('post.des/s');
            $op = input('post.op', '+');
            $order_sn = date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
            $current_time = time();

            if ($op == '-' && $money > $member['available_balance']) $money = $member['available_balance']; //扣除金额大于余额

            $add_log_data = [
                'member_id' => $memberId,
                'money' => $op . $money,
                'price' => 0,
                'order_sn' => $order_sn,
                'recharge_type' => 0,
                'create_time' => $current_time,
                'pay_time' => $current_time,
                'state' => 1,
                'des' => $des,
            ];

            $rechargeLogModel = model('common/member/RechargeLog');
            Db::startTrans();
            $res = $rechargeLogModel->add($add_log_data);
            if (!$res) {
                Db::rollback();
                ds_json_encode('err', '添加充值记录失败');
            }

            $desc = '后台充值';
            $update_available_balance_res = model('common/member/MemberLogic', 'logic')->updateAvailableBalance($memberId, $op . $money, 1, 1, $desc);
            if ($update_available_balance_res['code'] != SUCCESS) {
                Db::rollback();
                return;
            }
            Db::commit();

            ds_json_encode('ret', '充值成功');

        }


        $this->assign('data', $member);
        return $this->fetch();
    }


    /**
     * User:Simony
     * Function: 充值选项
     */
    public function options()
    {

        //where
        $where = [];

        //model
        $rechargeOptionsModel = model('common/member/RechargeOptions');

        $data = $rechargeOptionsModel->getList($where, 10, '*', 'sort desc');

        $this->assign('data', $data);
        $this->assign('states', $rechargeOptionsModel->states);
        $this->assign('page', $rechargeOptionsModel->page_info->render());
        return $this->fetch();
    }


    /**
     * User:Simony
     * Function:
     */
    public function optionAdd()
    {
        //params
        $id = input('param.id/d', '');

        //model
        $rechargeOptionsModel = model('common/member/RechargeOptions');

        if ($this->request->isAjax()) {
            $money = input('post.money/d', 0);
            $money <= 0 && ds_json_encode('err', '金币必须大于0');
            $price = input('post.price', 0);
            $price <= 0 && ds_json_encode('err', '金额必须大于0');

            //params
            $data = [
                'money' => $money,
                'price' => round($price, 2),
                'state' => input('post.state'),
                'sort' => input('post.sort'),
            ];

            $res = $id ? $rechargeOptionsModel->edit(['id' => $id], $data) : $rechargeOptionsModel->add($data);
            $res ? ds_json_encode('ret', '操作成功') : ds_json_encode('err', '操作失败');
        }

        //getData
        $data = $rechargeOptionsModel->getInfoByID($id);
        $this->assign('data', $data);
        return $this->fetch();
    }


}